//
//  TJTabBarController.swift
//  TJSinaWeibo
//
//  Created by 唐后能 on 16/4/8.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit

class TJTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // 添加tabBarVC的子控制器.
        setupChildVC()
        
        // 将tabBar更换成自定义的
//        tabBar = TJTabBar() //只读属性,不能直接赋值,需要用KVC.
        let customTabBar = TJTabBar()
        setValue(customTabBar, forKey: "tabBar")
        // 给自定义tabBar的闭包赋值.
        customTabBar.composeClosure = { () -> () in
        
            let composeVC = TJComposeVewController()
            let navVC = UINavigationController(rootViewController: composeVC)
            self.presentViewController(navVC, animated: true, completion: nil)
            
        }
        
    }
    
   // MARK: - 添加tabBarVC的子控制器.
    private func setupChildVC() {
    
        // 首页.
        let homeVC = TJHomeTableViewController()
        addchildVC(homeVC, imageName: "tabbar_home", title: "首页")
        // 消息.
        let messageVC = TJMessageTableViewController()
        addchildVC(messageVC, imageName: "tabbar_message_center", title: "消息")
        // 发现.
        let discoverVC = TJDiscoverTableViewController()
        addchildVC(discoverVC, imageName: "tabbar_discover", title: "发现")
        // 我.
        let profileVC = TJProfileTableViewController()
        addchildVC(profileVC, imageName: "tabbar_profile", title: "我")
    }
    
    private func addchildVC(controller:UIViewController,imageName:String,title:String) {
    
        controller.title = title
        controller.tabBarItem.image = UIImage(named: imageName)
        // 设置选中tabBar的图片及渲染模式(原始的).
        controller.tabBarItem.selectedImage = UIImage(named:imageName + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        // 设置tabBar字体属性.
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: .Normal)
        // 改变tabBar的背景颜色.
        self.tabBar.backgroundImage = UIImage(named: "tabbar_background")
    addChildViewController(UINavigationController(rootViewController:controller))
    }

}
